<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">项目名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin layui-btn-sm" lay-submit lay-filter="LAY-user-back-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm" title="刷新" data-type="refresh"><i
                        class="layui-icon layui-icon-refresh-3"></i>
                </button>
                                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm" title="添加" data-type="add"><i
                                        class="layui-icon layui-icon-add-1"></i>添加
                                </button>
                <!--                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm" title="删除" data-type="batchdel"><i-->
                <!--                        class="layui-icon layui-icon-delete"></i>删除-->
                <!--                </button>-->
            </div>
            <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>
            <script type="text/html" id="table-useradmin-admin">
                {{#  if(d.status == '2'  && d.task_status >= 2){ }}
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('project/ExamineBudgetDetail')?$auth->check('project/ExamineBudgetDetail'):'hide'}" lay-event="examine_budget_detail"><i
                        class="layui-icon layui-icon-edit"></i>查看详情</a>
<!--                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('Common/flowchart')?$auth->check('Common/flowchart'):'hide'}"-->
<!--                   lay-event="flowchart"><i class="iconfont icon-shenhe6"></i>查看流程图</a>-->
                {{#  } else if(d.status == '1'  && d.task_status < 2) { }}
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('project/ExamineBudgetDetail')?$auth->check('project/ExamineBudgetDetail'):'hide'}" lay-event="examine_budget_detail"><i
                        class="layui-icon layui-icon-edit"></i>查看详情</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="examine_budget_edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                {{#  } else if(d.status == '1'  && d.task_status >= 2) { }}
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('project/ExamineBudgetDetail')?$auth->check('project/ExamineBudgetDetail'):'hide'}" lay-event="examine_budget_detail"><i
                        class="layui-icon layui-icon-edit"></i>查看详情</a>
<!--                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('Common/flowchart')?$auth->check('Common/flowchart'):'hide'}"-->
<!--                   lay-event="flowchart"><i class="iconfont icon-shenhe6"></i>查看流程图</a>-->
                {{#  } else if(d.status == '-1') { }}
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('project/AgainBudget')?$auth->check('project/AgainBudget'):'hide'}" lay-event="again_budget"><i
                        class="layui-icon layui-icon-edit"></i>重新预算</a>
                {{#  } else if(d.budget_status == '1') { }}
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('project/projectBudget')?$auth->check('project/projectBudget'):'hide'}" lay-event="budget"><i
                        class="layui-icon layui-icon-edit"></i>预算</a>

                {{#  } else { }}
                <a class="layui-btn layui-btn-normal layui-btn-xs"><i class="layui-icon layui-icon-edit"></i>审核中....</a>
                {{#  } }}
            </script>
        </div>
    </div>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    var listUrl = "{:url('project/examine')}", create = "{:url('project/budget_add')}",
        examineRead = "{:url('project/examineRead')}",
        deleteUrl = "{:url('project/del')}", detailContent = "{:url('project/DetailContent')}",
        projectBudget = "{:url('project/projectBudget')}", ExamineBudget = "{:url('project/ExamineBudget')}",
        refuseProject = "{:url('project/refuseProject')}",ExamineBudgetDetail = "{:url('project/ExamineBudgetDetail')}",ExamineBudgetEdit = "{:url('project/ExamineBudgetEdit')}",AgainBudget = "{:url('project/AgainBudget')}",flowchart = "{:url('Common/flowchart')}";
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;
        table.render({
            elem: "#LAY-user-back-manage", url: listUrl, even: true
            , cellMinWidth: 100  ,size:"sm"
            , cols: [
                [
                    {type: "checkbox", fixed: "left"},
                    {field: "id", align: "center", title: "id"},
                    {field: "name", align: "center", title: "项目名称", event: 'setSign'},
                    {
                        field: "nickname", align: "center", title: "项目创建人", templet: function (e) {
                            if (e.admin != null) {
                                return e.admin.nickname;
                            } else {
                                return "Unknown";
                            }
                        }
                    },
                    {field: "period", align: "center", title: "项目周期"},
                    {field: "yuefen", align: "center", title: "预算月份"},
                    {
                        field: "group_id", align: "center", title: "所属部门", templet: function (e) {
                            if (e.AuthGroup != null) {
                                return e.AuthGroup.name;
                            } else {
                                return "Unknown";
                            }
                        }
                    },
                    {field: "progress_status", align: "center", title: "项目进度", event: 'progress_status'},
                    {title: "操作", align: "center", fixed: "right", toolbar: "#table-useradmin-admin"}]],
            response: {
                statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
            },
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code,
                    "count": res.total, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
            text: {
                none: '呀！一条数据都没有'
            },
            page: true,
            limit: 20,
        });
        //按钮触发
        table.on("tool(LAY-user-back-manage)", function (e) {
            var param = $.param({'id': e.data.id});
            if ("examine" === e.event) {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '审核项目'
                    , content: examineRead + '?' + param
                    , area: ['100%', '100%']
                    , maxmin: true
                    , btn: ['立项', '拒绝']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: examineRead + '?' + param,
                                type: 'post',
                                data: field,
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.close(index); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }, btn2: function (index, layero) {
                        layer.confirm("确定拒绝该项目吗？", function (t) {
                            layer.prompt({
                                formType: 2,
                                value: '',
                                title: '请输入拒绝原因',
                                area: ['300px', '350px'] //自定义文本域宽高
                            }, function (value, index, elem) {
                                $.ajax({
                                    url: "{:url('project/errorProject')}",
                                    data: {'id': e.data.id, "reasons": value},
                                    type: "post",
                                    dataType: "json",
                                    success: function (res) {
                                        if (res.code === 200) {
                                            layer.msg(res.msg, {icon: 1});
                                            table.reload('LAY-user-back-manage');
                                            layer.close(t); //关闭弹层
                                        } else {
                                            console.log(res);
                                            layer.msg(res.msg, {icon: 5});
                                            return false;
                                        }
                                    }
                                });
                                layer.closeAll();
                            });
                        });
                    }
                });
            } else if ("budget" === e.event) {
                var params = $.param({'ids': e.data.id});
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '项目预算'
                    , content: projectBudget + '?' + param
                    , area: ['100%', '100%']
                    , maxmin: true
                    , btn: ['确认', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: projectBudget + '?' + param,
                                type: 'post',
                                data: field,
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.close(index); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    },
                });
            } else if ("setSign" === e.event) {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '预览'
                    , content: detailContent + '?' + param
                    , area: ['100%', '100%']
                    , maxmin: true
                });
            } else if ("examine_budget" === e.event) {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '预算审核'
                    , content: ExamineBudget + '?' + param
                    , area: ['100%', '100%']
                    , maxmin: true
                    , btn: ['确认', '拒绝']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            field['type'] = 'pass';

                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: ExamineBudget + '?' + param,
                                type: 'post',
                                data: field,
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.close(index); //关闭弹层
                                    } else {
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                    , btn2: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            field['type'] = 'refuse';

                            //提交 Ajax 成功后，静态更新表格中的数据
                            layer.confirm("确定拒绝该项目吗？", function (t) {
                                $.ajax({
                                    url: ExamineBudget + '?' + param,
                                    data: field,
                                    type: "post",
                                    dataType: "json",
                                    success: function (res) {
                                        if (res.code === 200) {
                                            layer.msg(res.msg, {icon: 1});
                                            table.reload('LAY-user-back-manage');
                                            layer.close(t); //关闭弹层
                                        } else {
                                            console.log(res);
                                            layer.msg(res.msg, {icon: 5});
                                            return false;
                                        }
                                    }
                                });
                                layer.closeAll();
                            });
                        });
                        submit.trigger('click');
                        return false
                    }
                });
            } else if ("again_budget" === e.event) {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '重新预算'
                    , content: AgainBudget + '?' + param
                    , area: ['100%', '100%']
                    , maxmin: true
                    , btn: ['确认', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: AgainBudget + '?' + param,
                                type: 'post',
                                data: field,
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.close(index); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    },
                });
            } else if ("examine_budget_detail" === e.event) {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '查看详情'
                    , content: ExamineBudgetDetail + '?' + param
                    , area: ['100%', '100%']
                    , maxmin: true
                });
            }else if ("examine_budget_edit" === e.event){
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '编辑页面'
                    , content: ExamineBudgetEdit + '?' + param
                    , area: ['100%', '100%']
                    , maxmin: true
                    , btn: ['确认', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: ExamineBudgetEdit + '?' + param,
                                type: 'post',
                                data: field,
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.close(index); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    },
                });
            } else if ("flowchart" == e.event) {
                var params = $.param({'id': e.data.id, 'table': 'budget'});
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '查看流程图'
                    , content: flowchart + '?' + params
                    , area: ['100%', '100%']
                    , maxmin: true
                });
            }
        });

        //监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-user-back-manage', {
                where: field
            });
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-user-back-manage')
                    , checkData = checkStatus.data; //得到选中的数据
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                var id = ''; //task_id
                for (var i = 0; i < checkData.length; i++) {
                    id += checkData[i].id + ",";
                }
                id = id.substring(0, id.length - 1);
                var ids = $.param({'ids': id});
                layer.confirm('确定删除吗？', function (index) {
                    //执行 Ajax 后重载
                    $.ajax({
                        url: deleteUrl,
                        type: "post",
                        data: ids,
                        success: function (res) {
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('LAY-user-back-manage');
                                layer.close(index); //关闭弹层
                            } else {
                                console.log(res);
                                layer.msg(res.msg, {icon: 5});
                                return false;
                            }
                        }
                    });
                    // table.reload('LAY-user-back-manage');
                    // layer.msg('已删除');
                });
            }
            , add: function () {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '添加'
                    , content: create
                    , area: ['50%', '50%']
                    , maxmin: true
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            console.log(field)
                            $.ajax({
                                url: create,
                                type: 'post',
                                data: field,
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.close(index); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            }, refresh: function () {
                table.reload('LAY-user-back-manage');
            }
        };
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
